package com.ruoyi.web.controller.gallery;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.supe.HomeCarousel;
import com.ruoyi.system.domain.supe.Substation;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.gallery.HomeCarouselService;
import com.ruoyi.system.service.gallery.SubstationService;
import com.ruoyi.system.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@RestController
@RequestMapping("/fenzhan/carousel")
public class HomeCarouselController extends BaseController {


    @Autowired
    private HomeCarouselService homeCarouselService;
    @Autowired
    private SubstationService substationService;
    @Autowired(required = false)
    private TokenService tokenService;
    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 查询首页轮播图列表
     */
    @PreAuthorize("@ss.hasPermi('fenzhan:carousel:list')")
    @GetMapping("/list")
    public TableDataInfo list(HomeCarousel homeCarousel)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        homeCarousel.setUserId(userId);
        List<HomeCarousel> list = homeCarouselService.selectHomeCarouselList(homeCarousel);
        return getDataTable(list);
    }

    /**
     * 导出首页轮播图列表
     */
    @PreAuthorize("@ss.hasPermi('fenzhan:carousel:export')")
    @Log(title = "首页轮播图", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HomeCarousel homeCarousel)
    {
        List<HomeCarousel> list = homeCarouselService.selectHomeCarouselList(homeCarousel);
        ExcelUtil<HomeCarousel> util = new ExcelUtil<HomeCarousel>(HomeCarousel.class);
        return util.exportExcel(list, "carousel");
    }

    /**
     * 获取首页轮播图详细信息
     */
    @PreAuthorize("@ss.hasPermi('fenzhan:carousel:query')")
    @GetMapping(value = "/{carouselId}")
    public AjaxResult getInfo(@PathVariable("carouselId") Long carouselId)
    {
        return AjaxResult.success(homeCarouselService.selectHomeCarouselById(carouselId));
    }

    /**
     * 新增首页轮播图
     */
    @PreAuthorize("@ss.hasPermi('fenzhan:carousel:add')")
    @Log(title = "首页轮播图", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HomeCarousel homeCarousel)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        Substation subById = substationService.findSubById(userId);
        Map<String,Long> map = new HashMap<String, Long>();
        map.put("userId",subById.getUserId());
        map.put("deptId",subById.getDeptId());
        map.put("substationId",subById.getSubstationId());
        homeCarousel.setUserId(map.get("userId"));
        homeCarousel.setDeptId(map.get("deptId"));
        homeCarousel.setSubstationId(map.get("substationId"));
        SysUser aById = sysUserMapper.findAById(userId);
        homeCarousel.setCarouselArea(aById.getDept().getDeptName());
        return toAjax(homeCarouselService.insertHomeCarousel(homeCarousel));
    }

    /**
     * 修改首页轮播图
     */
    @PreAuthorize("@ss.hasPermi('fenzhan:carousel:edit')")
    @Log(title = "首页轮播图", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HomeCarousel homeCarousel)
    {
        return toAjax(homeCarouselService.updateHomeCarousel(homeCarousel));
    }

    /**
     * 删除首页轮播图
     */
    @PreAuthorize("@ss.hasPermi('fenzhan:carousel:remove')")
    @Log(title = "首页轮播图", businessType = BusinessType.DELETE)
    @DeleteMapping("/{carouselIds}")
    public AjaxResult remove(@PathVariable Long[] carouselIds)
    {
        return toAjax(homeCarouselService.deleteHomeCarouselByIds(carouselIds));
    }

    /**
     * 上传图片
     * @param file
     * @return
     */
    @PostMapping("/uploadImage")
    public Map<String,Object> up(MultipartFile file){
        String url = "/home/ruoyi/profile/xiazai";
        return FileUtil.uploadImage(file,url);
    }
}
